package com.support.widget.pulltorefresh.pullableview;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.ViewGroup;

import com.support.widget.pulltorefresh.interfacelistener.Pullable;

/**
 * Created by jiangyu on 15/9/29.
 */
public class PullableRecyclerView extends RecyclerView implements Pullable {

    public PullableRecyclerView(Context context) {
        super(context);
    }

    public PullableRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullableRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean canPullDown() {
        return shouldHandleRefresh();
    }

    @Override
    public boolean canPullUp() {
        return shouldHandleRecyclerViewLoadingMore();
    }

    /**
     * 是否满足处理刷新的条件
     *
     * @return
     */
    private boolean shouldHandleRefresh() {
        int firstChildTop = 0;
        if (getChildCount() > 0) {
            // 如果RecyclerView的子控件数量不为0，获取第一个子控件的top

            // 解决item的topMargin不为0时不能触发下拉刷新
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getChildAt(0).getLayoutParams();
            firstChildTop = getChildAt(0).getTop() - layoutParams.topMargin - getPaddingTop();
        }

        LayoutManager manager = getLayoutManager();
        if (manager == null) {
            return true;
        }
        if (manager.getItemCount() == 0) {
            return true;
        }

        if (manager instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
            if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0 && firstChildTop == 0) {
                return true;
            }
        } else if (manager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) manager;

            int[] out = layoutManager.findFirstCompletelyVisibleItemPositions(null);
            if (out[0] == 0) {
                return true;
            }
        }
        return false;
    }

    private boolean shouldHandleRecyclerViewLoadingMore() {
        LayoutManager manager = getLayoutManager();

        if (manager instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
            if(manager != null && getAdapter() != null) {
                if (layoutManager.findLastCompletelyVisibleItemPosition() == getAdapter().getItemCount() - 1) {
                    return true;
                }
            }
        } else if (manager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) manager;

            int[] out = layoutManager.findLastCompletelyVisibleItemPositions(null);
            int lastPosition = layoutManager.getItemCount() - 1;
            for (int position : out) {
                if (position == lastPosition) {
                    return true;
                }
            }
        }
        return false;
    }
}
